package org.dromara.manu.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.manu.domain.vo.ManuMaterialMasterVo;
import org.dromara.manu.domain.bo.ManuMaterialMasterBo;
import org.dromara.manu.service.IManuMaterialMasterService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 变更主数据
 *
 * @author Bin Liu
 * @date 2024-11-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manu/materialMaster")
public class ManuMaterialMasterController extends BaseController {

    private final IManuMaterialMasterService manuMaterialMasterService;

    /**
     * 查询变更主数据列表
     */
    @SaCheckPermission("manu:materialMaster:list")
    @GetMapping("/list")
    public TableDataInfo<ManuMaterialMasterVo> list(ManuMaterialMasterBo bo, PageQuery pageQuery) {
        return manuMaterialMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出变更主数据列表
     */
    @SaCheckPermission("manu:materialMaster:export")
    @Log(title = "变更主数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ManuMaterialMasterBo bo, HttpServletResponse response) {
        List<ManuMaterialMasterVo> list = manuMaterialMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "变更主数据", ManuMaterialMasterVo.class, response);
    }

    /**
     * 获取变更主数据详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("manu:materialMaster:query")
    @GetMapping("/{id}")
    public R<ManuMaterialMasterVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(manuMaterialMasterService.queryById(id));
    }

    /**
     * 新增变更主数据
     */
    @SaCheckPermission("manu:materialMaster:add")
    @Log(title = "变更主数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ManuMaterialMasterBo bo) {
        return toAjax(manuMaterialMasterService.insertByBo(bo));
    }

    /**
     * 修改变更主数据
     */
    @SaCheckPermission("manu:materialMaster:edit")
    @Log(title = "变更主数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ManuMaterialMasterBo bo) {
        return toAjax(manuMaterialMasterService.updateByBo(bo));
    }

    /**
     * 删除变更主数据
     *
     * @param ids 主键串
     */
    @SaCheckPermission("manu:materialMaster:remove")
    @Log(title = "变更主数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(manuMaterialMasterService.deleteWithValidByIds(List.of(ids), true));
    }

    @SaIgnore
    @GetMapping("/deliveryDateInquire/{id}")
    public R<Void> deliveryDateInquire(@PathVariable Long id) {
        manuMaterialMasterService.deliveryDateInquire(id);
        return R.ok();
    }
}
